// pages/coach/nearby/nearby.js
let common = require('../../../utils/common.js');
Page({
  data: {
    location: {
      longitude: 106.55155600,
      latitude: 29.56301000
    },
    markers: [],
    covers:[],
    polyline: [{
      points: [{
        longitude: 113.3245211,
        latitude: 23.10229
      }, {
        longitude: 113.324520,
        latitude: 23.21229
      }],
      color: "#000",
      width: 5,
      dottedLine: true
    }],
    controls: [{
      id: 1,
      iconPath: '/icon/down.png',
      position: {
        left: 0,
        top: 300 - 50,
        width: 28,
        height: 17
      },
      clickable: true
    }]
  },
  regionchange(e) {
    console.log(e)
  },
  markertap(e) {
    // wx.showModal({content:e.markerId.toString()})
    var id = e.markerId;
    wx.navigateTo({ url: '../detail/detail?id=' + id });
  },
  controltap(e) {
    console.log(e.controlId)
  },
  outtap(e) {
    // wx.showModal(JSON.stringify(e));
    var id = e.markerId;
    wx.navigateTo({ url: '../detail/detail?id=' + id });
  },
  onLoad: function (options) {
    
  },
  onReady: function () {
    // 页面渲染完成

  },
  onShow: function () {
    // 页面显示
    // 页面初始化 options为页面跳转所带来的参数
    let that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        that.setData({
          'location.longitude': res.longitude,
          'location.latitude': res.latitude
        });
      },
      complete: function (res) {
        common.asynPost(common.API.user.nearby, {
          longitude: that.data.location.longitude,
          latitude: that.data.location.latitude
        }, function (data) {
          if (data.coaches) {
            var tempMarkers = [];
            for (var i = 0, len = data.coaches.length; i < len; i++) {
              var coach = data.coaches[i];
              tempMarkers.push({
                iconPath: "/icon/location.png",
                id: coach.id,
                latitude: coach.latitude,
                longitude: coach.longitude,
                width: 28,
                height: 28,
                callout: {
                  content: coach.name + "(" + coach.typeName + ")",
                  display: 'ALWAYS',
                  color: '#646464',
                  fontSize: 12,
                  padding: 3,
                }
              });
            }
            that.setData({
              markers:tempMarkers,
              covers:tempMarkers
            })
          }
        });
      }
    });
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})